package com.jwthhealth_pub.old.signutil.db;

import android.content.Context;
import android.content.res.AssetManager;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class SQLdm {
    public static final String FILECITIES = "filecities";
    public static final String FILEADDRESS = "fileaddress";

//    private static final String fileCitiesPath = "data/data/com.jwthhealth/cities.db";
    private static final String fileAddressPath = "data/data/com.jwthhealth/address.db";
    private static final String pathStr = "data/data/com.jwthhealth";
        
    SQLiteDatabase database;

    public  SQLiteDatabase openDatabase(Context context, String targetPath){

        File jhPath;

//        if (targetPath.equals(FILECITIES)) {
//            jhPath = new File(fileCitiesPath);
//        } else {
//        }
        jhPath = new File(fileAddressPath);

            if(jhPath.exists()){
                return SQLiteDatabase.openOrCreateDatabase(jhPath, null);
            }else{    
                File path=new File(pathStr);    
                if (path.mkdir()){
                }else{    
                };    
                try {    
                    AssetManager am= context.getAssets();    
                    InputStream is=am.open("address.db");
                    Log.i("test", is+"");  
                    FileOutputStream fos=new FileOutputStream(jhPath);
                    Log.i("test", "fos="+fos);  
                    Log.i("test", "jhPath="+ jhPath);
                    byte[] buffer=new byte[1024];    
                    int count = 0;    
                    while((count = is.read(buffer))>0){    
                        fos.write(buffer,0,count);    
                    }    
                    fos.flush();    
                    fos.close();    
                    is.close();    
                } catch (IOException e) {    
                    e.printStackTrace();    
                    return null;  
                }    
                return openDatabase(context, targetPath);
            }    
    }    
}
